

package scrum.gui.project;


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import scrum.entity.UserStory;
import scrum.gui.help.Logic;




public class UserStoryDialog extends JDialog {
    
   
	private static final long serialVersionUID = 1L;
	
	private GeneralPane generalPane;
	int mode;
	
	public UserStoryDialog(GeneralPane generalPane,boolean modal,int mode) {
		
		this.generalPane = generalPane;
		this.mode = mode;
        initComponents();
    }
    
	public void setDate(){
		startDateUserStory.setValue(new Date());
		dueDateUserStory.setValue(new Date());
	}
  
    private void initComponents() {
    	
        GridBagConstraints gridBagConstraints;

        jScrollPane = new JScrollPane();
        jTextAreaUserStory = new JTextArea();
        btnSave = new JButton();
        btnClose = new JButton();
        
        
        jLabelResponsible = new javax.swing.JLabel();
		jLabelProgress = new javax.swing.JLabel();
		jLabelStratUserStory = new javax.swing.JLabel();
		jLabelDueUserSotry = new javax.swing.JLabel();
		jLabelBussinessValue = new javax.swing.JLabel();
		jLabelWeight = new javax.swing.JLabel();
		jTextFieldProgress = new javax.swing.JTextField();
		jTextFieldResponsible = new javax.swing.JTextField();
		jTextFieldBussinessValue = new javax.swing.JTextField();
		jTextFieldWeight = new javax.swing.JTextField();
		dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(true);
		startDateUserStory = new javax.swing.JFormattedTextField(dateFormat);
		dueDateUserStory = new javax.swing.JFormattedTextField(dateFormat);
		jPanelDetails = new javax.swing.JPanel();

        getContentPane().setLayout(new GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        
        
        
        jScrollPane.setBorder(new javax.swing.border.TitledBorder("UserStory"));
 
        
        jTextAreaUserStory.setEditable(true);
        jTextAreaUserStory.setColumns(35);
        jTextAreaUserStory.setTabSize(2);
        jTextAreaUserStory.setRows(5);
        jTextAreaUserStory.setLineWrap(true);
        jTextAreaUserStory.setWrapStyleWord(true);
        jScrollPane.setViewportView(jTextAreaUserStory);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane, gridBagConstraints);
        
        

    	
		jPanelDetails.setLayout(new java.awt.GridBagLayout());
		jPanelDetails.setBorder(new javax.swing.border.TitledBorder(
				"StoryDetails"));
		gridBagConstraints = new java.awt.GridBagConstraints();
		
		jLabelResponsible.setText("Responsible");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelResponsible,gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(jTextFieldResponsible, gridBagConstraints);
		
		
		
		
		jLabelProgress.setText("Progress");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelProgress, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(jTextFieldProgress, gridBagConstraints);
		
		jLabelStratUserStory.setText("StartDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelStratUserStory, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(startDateUserStory, gridBagConstraints);
		
		
		
		jLabelDueUserSotry.setText("DueDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelDueUserSotry, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(dueDateUserStory, gridBagConstraints);
		

		jLabelBussinessValue.setText("BussinessValue");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelBussinessValue, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(jTextFieldBussinessValue, gridBagConstraints);
		
		jLabelWeight.setText("WeightValue");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelWeight, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0.5;
		jPanelDetails.add(jTextFieldWeight, gridBagConstraints);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1;
		getContentPane().add(jPanelDetails, gridBagConstraints);

		
		

        btnSave.setText("Ok");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1;
        getContentPane().add(btnSave, gridBagConstraints);

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1;
        getContentPane().add(btnClose, gridBagConstraints);
        
        if(mode == 1){
        	setDate();
        }
        
        if(mode == 2){
        	
        	setStory(generalPane.currentStory);
        }

        pack();
    }
    
    private void setStory(UserStory userStory){
    	
    	this.jTextAreaUserStory.setText(userStory.getStory());
    	this.jTextFieldResponsible.setText(userStory.getResponsible());
    	this.jTextFieldBussinessValue.setText(userStory.getBusinessValue());
    	this.jTextFieldProgress.setText(String.valueOf(userStory.getProgress()));
    	this.startDateUserStory.setText(userStory.getStartDate());
    	this.dueDateUserStory.setText(userStory.getDueDate());
    	this.jTextFieldWeight.setText(userStory.getWeight());
    }
    
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
    	
		UserStory userStory = new UserStory();

		boolean b = false;

		if (!this.jTextFieldProgress.getText().equals("")) {

			int value = 0;
			try {
				value = Integer.parseInt(this.jTextFieldProgress.getText());
			} catch (NumberFormatException e) {
				b = true;
				JOptionPane.showMessageDialog(this, "You should input a interger between 0 to 100", "Warning",
						JOptionPane.WARNING_MESSAGE);
			}
			
			userStory.setProgress(value);
		}
		if(!this.jTextFieldBussinessValue.getText().equals("")){

		
			try {
				Integer.parseInt(this.jTextFieldBussinessValue.getText());
			} catch (NumberFormatException e) {
				b = true;
				JOptionPane.showMessageDialog(this, "You should input a interger in bussiness value between 0 to 10", "Warning",
						JOptionPane.WARNING_MESSAGE);
			}
			
		}
		if(!this.jTextFieldWeight.getText().equals("")){

			
			try {
				Integer.parseInt(this.jTextFieldWeight.getText());
			} catch (NumberFormatException e) {
				b = true;
				JOptionPane.showMessageDialog(this, "You should input a interger in weight between 0 to 10", "Warning",
						JOptionPane.WARNING_MESSAGE);
			}
			
		}
		if (!b) {
			
			Logic logic = new Logic();

			userStory.setStory(this.jTextAreaUserStory.getText());
			userStory.setResponsible(this.jTextFieldResponsible.getText());
			userStory.setBusinessValue(this.jTextFieldBussinessValue.getText());

			userStory.setStartDate(this.startDateUserStory.getText());
			userStory.setDueDate(this.dueDateUserStory.getText());
			userStory.setWeight(this.jTextFieldWeight.getText());

			String message = logic.checkUserStory(generalPane.currentProject,
					userStory);

			if (message == null) {

				if (mode == 1) {
					generalPane.addNewUserStory(userStory);
					generalPane.currentStory = userStory;
					generalPane.clearStoryDetails();
					generalPane.showStoryDetails();
					generalPane.setModifyMode(true);

				} else {
					generalPane.saveStoryModification(userStory,
							generalPane.getIndex(generalPane.currentStory));
					generalPane.currentStory = userStory;
					generalPane.clearStoryDetails();
					generalPane.showStoryDetails();

				}
				closeDialog(null);
			} else {
				JOptionPane.showMessageDialog(this, message, "Warning",
						JOptionPane.WARNING_MESSAGE);
			}
		}
    	
    }
    
    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
        closeDialog(null);
    }
    
    /** Closes the dialog */
    private void closeDialog(WindowEvent evt) {
        setVisible(false);
        dispose();
    }
    
   
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTextArea jTextAreaUserStory;
    
    private javax.swing.JLabel jLabelResponsible;
	private javax.swing.JLabel jLabelProgress;
	private javax.swing.JLabel jLabelStratUserStory;
	private javax.swing.JLabel jLabelDueUserSotry;
	private javax.swing.JLabel jLabelBussinessValue;
	private javax.swing.JLabel jLabelWeight;
	
	private JFormattedTextField startDateUserStory;
	private JFormattedTextField dueDateUserStory;
	private javax.swing.JTextField jTextFieldProgress;
	private javax.swing.JTextField jTextFieldResponsible;
	private javax.swing.JTextField jTextFieldBussinessValue;
	private javax.swing.JTextField jTextFieldWeight;
	private DateFormat dateFormat;
	private javax.swing.JPanel jPanelDetails;
   
}
